<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests\RuleRequest;
use App\Http\Controllers\Controller;
use App\Model\Rule;
use DB;
class RuleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $rules = Rule::where('status','<>', '-1')
                ->orderBy('name')
                ->paginate(15);
        
        return view('admin.rule.index', compact('rules'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.rule.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(RuleRequest $request)
    {
        $data = $request->only(['name', 'title', 'status']);
        $res = Rule::insert($data);
        if(false !== $res){
            return redirect('/admin/rule');
        } else {
            return redirect('/tips')->with(['info'=>'权限添加失败', 'url'=>url('/admin/rule/create')]);
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $rule = Rule::where('id', $id)->first();
        return view('admin.rule.edit', compact('rule'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(RuleRequest $request, $id)
    {
        $data = $request->only(['name','title','status']);
        $res = Rule::where('id', $id)->update($data);
        if(false !== $res){
            return redirect('/tips')->with(['info'=>'修改成功', 'url'=>url('/admin/rule')]);
        } else {
            return redirect('/tips')->with(['info'=>'修改失败', 'url'=>$_SERVER['HTTP_REFERER']]);
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        
    }
    
    public function delete(Request $request)
    {
        $id = $request->get('id');
        $res = Rule::whereIn('id', $id)->delete();
        if(false !== $res){
            // 删除分组规则表中相关的数据
            $rules = DB::table('group_rule')->lists('rules','id');
            foreach($rules as $key=>$rule){
                $newrule = explode(',', $rule);
                foreach($id as $v){
                    if(false !== $kv = array_search($v, $newrule)){
                        unset($newrule[$kv]);
                    }
                }
                // 组装
                $newrule = implode(',', $newrule);
                // 更新数据
                DB::table('group_rule')->where('id', $key)->update(['rules'=>$newrule]);
            }
            return response()->json(['status'=>1, 'info'=>'删除成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>'删除失败']);
        }
    }
    
    public function multi(Request $request)
    {
        $status = $request->get('status');
        $id = $request->get('id');
        $res = Rule::whereIn('id', $id)->update(['status'=>$status]);
        if(false !== $res){
            return response()->json(['status'=>1, 'info'=>'操作成功']);
        } else {
            return response()->json(['status'=>0, 'info'=>'操作失败']);
        }
    }
}
